package com.ssyt.hiring.web.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ssyt.cdp.common.PageModel;
import com.ssyt.hiring.entity.ApplicationEntity;
import com.ssyt.hiring.entity.CompanyEntity;
import com.ssyt.hiring.entity.PositionEntity;
import com.ssyt.hiring.web.service.CompanyService;

@Controller
@Scope("prototype")
public class CompanyAction extends CommonAction {

	private static final int FIRST_PAGE = 1;
	private static final int DEFAULT_PAGE_SIZE = 10;
	@Autowired
	private CompanyService companySevice;
	private CompanyEntity company;

	private String validateCode;
	private List<PositionEntity> positionList;
	private PageModel<ApplicationEntity> applicationPageModel;
	private int pageNo;
	private int pageSize;

	/**
	 * 企业注册
	 * 
	 * @return
	 */
	public String companyRegister() {
		companySevice.companyregister(company);
		return "company-login";
	}

	/**
	 * 检查注册时code是否重复
	 */
	public void checkCompanyCode() {
		boolean result = companySevice.checkCompanyCode(company.getCode());
		print(result + "");
	}

	/**
	 * 企业登录
	 * 
	 * @return
	 */
	// public String companyLogin() {
	// String rand = (String) ServletActionContext.getRequest().getSession()
	// .getAttribute("rand");
	// if (rand.equals(validateCode)) {
	// company = companySevice.companyLogin(company);
	// ServletActionContext.getRequest().getSession()
	// .setAttribute("company", company);
	// return "companyLoginSuccess";
	// } else {
	// return "company-login";
	// }
	// }

	/**
	 * 企业登录
	 */
	public String companyLogin() {
		company = companySevice.companyLogin(company);
		if (company != null) {
			ServletActionContext.getRequest().getSession()
					.setAttribute("company", company);
			positionList = positionList();
			applicationPageModel = applicationPageModel();
			return "companyLoginSuccess";
		} else {
			return "company-login";
		}
	}

	/**
	 * 公司注销
	 */
	public String companyLoginout() {
		ServletActionContext.getRequest().getSession().invalidate();
		return "Logout";
	}

	/**
	 * 企业信息查询
	 * 
	 * @return
	 */
	public String getInfoCompany() {
		int companyId = getCompanyId();
		company = companySevice.getInfoCompany(companyId);
		return "company-info";
	}

	public int getCompanyId() {
		CompanyEntity company = (CompanyEntity) ServletActionContext
				.getRequest().getSession().getAttribute("company");
		return company.getId();
	}

	/**
	 * 企业信息修改
	 * 
	 * @return
	 */
	public String updateInfoCompany() {
		companySevice.updateInfoCompany(company);
		return "companyLoginSuccess";
	}

	/**
	 * 查询出公司对应的发布的所有职位
	 * 
	 * @return
	 */
	public List<PositionEntity> positionList() {
		int companyId = getCompanyId();
		return companySevice.positionList(companyId);
	}

	/**
	 * 查询所有投递的简历
	 */
	public PageModel<ApplicationEntity> applicationPageModel() {
		if (pageNo == 0) {
			pageNo = FIRST_PAGE;
		}
		if (pageSize == 0) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		applicationPageModel = companySevice.applicationPageModel(pageNo,
				pageSize);
		return applicationPageModel;
	}

	/**
	 * 刷新重加载
	 * 
	 * @return
	 */

	public String positionListAll() {
		positionList = positionList();
		applicationPageModel = applicationPageModel();
		return "companyLoginSuccess";
	}

	public PageModel<ApplicationEntity> getApplicationPageModel() {
		return applicationPageModel;
	}

	public void setApplicationPageModel(
			PageModel<ApplicationEntity> applicationPageModel) {
		this.applicationPageModel = applicationPageModel;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public CompanyEntity getCompany() {
		return company;
	}

	public void setCompany(CompanyEntity company) {
		this.company = company;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	public List<PositionEntity> getPositionList() {
		return positionList;
	}

	public void setPositionList(List<PositionEntity> positionList) {
		this.positionList = positionList;
	}

}
